-- Library: Option Templates
-- Author: Kar (EU-Silvermoon)

local format = format
local tonumber = tonumber

local borders = {}
borders["TOPLEFT"] = {}
borders["TOPLEFT"]["L"] = 0 / 16
borders["TOPLEFT"]["R"] = 14 / 16
borders["TOPLEFT"]["T"] = 0 / 16
borders["TOPLEFT"]["B"] = 14 / 16

borders["TOPRIGHT"] = {}
borders["TOPRIGHT"]["L"] = 14 / 16
borders["TOPRIGHT"]["R"] = 0 / 16
borders["TOPRIGHT"]["T"] = 0 / 16
borders["TOPRIGHT"]["B"] = 14 / 16

borders["BOTTOMLEFT"] = {}
borders["BOTTOMLEFT"]["L"] = 0 / 16
borders["BOTTOMLEFT"]["R"] = 14 / 16
borders["BOTTOMLEFT"]["T"] = 14 / 16
borders["BOTTOMLEFT"]["B"] = 0 / 16

borders["BOTTOMRIGHT"] = {}
borders["BOTTOMRIGHT"]["L"] = 14 / 16
borders["BOTTOMRIGHT"]["R"] = 0 / 16
borders["BOTTOMRIGHT"]["T"] = 14 / 16
borders["BOTTOMRIGHT"]["B"] = 0 / 16

borders["TOP"] = {}
borders["TOP"]["L"] = 8 / 16
borders["TOP"]["R"] = 16 / 16
borders["TOP"]["T"] = 0 / 16
borders["TOP"]["B"] = 6 / 16

borders["BOTTOM"] = {}
borders["BOTTOM"]["L"] = 8 / 16
borders["BOTTOM"]["R"] = 16 / 16
borders["BOTTOM"]["T"] = 6 / 16
borders["BOTTOM"]["B"] = 0 / 16

borders["LEFT"] = {}
borders["LEFT"]["L"] = 0 / 16
borders["LEFT"]["R"] = 6 / 16
borders["LEFT"]["T"] = 8 / 16
borders["LEFT"]["B"] = 16 / 16

borders["RIGHT"] = {}
borders["RIGHT"]["L"] = 6 / 16
borders["RIGHT"]["R"] = 0 / 16
borders["RIGHT"]["T"] = 8 / 16
borders["RIGHT"]["B"] = 16 / 16

local optionMenus = {}
local sideMenus = {}
local topMenu
local menuPanel_Seperated

---------------------
-- Slider Template --
---------------------

function Slider_Template_OnLoad(self)

	self.EditBox:HideBorder()
	BorderTemplates_CreateBorder(self)
	
	if self:GetAttribute("max") then
		if self:GetAttribute("min") then
			self.Slider:SetMinMaxValues(self:GetAttribute("min"),self:GetAttribute("max"))
		end
	end
	
	if self:GetAttribute("step") then
		self.Slider:SetValueStep(self:GetAttribute("step"))
	end
	
	if self:GetAttribute("round") then
		self.Slider.Round = self:GetAttribute("round")
	end
	
	if not self.Slider.Round then
		self.Slider.Round = 0
	end
	
	if self:GetAttribute("value") then
		self.Slider:SetValue(self:GetAttribute("value"))
		self.EditBox:SetText(format("%."..self.Slider.Round.."f",self:GetAttribute("value")))
	end
	
	if self:GetAttribute("func") then
		self.Slider.Func = _G[self:GetAttribute("func")]
	end
	
	self.Slider:SetScript("OnValueChanged", Slider_Template_OnValueChanged)

	local low, high = self.Slider:GetMinMaxValues()
	self.Slider.Low:SetText(low)
	self.Slider.High:SetText(high)
	self.Slider.Texture:SetVertexColor(1.0,1.0,1.0)
	self.EditBox.OnEnterPressed = Slider_Template_EditBox_OnEnterPressed
	self.EditBox.OnEscapePressed = Slider_Template_EditBox_OnEscapePressed

end

function Slider_Template_OnValueChanged(self)

	self:GetParent().EditBox:SetText(format("%."..self.Round.."f",self:GetValue()))

	if self.Func then
		self.Func(self)
	end

end

function Slider_Template_OnEnter(self)

	 if self.TooltipTitle then
		OptionTemplates_Tooltip:SetOwner(self,"ANCHOR_RIGHT")
		OptionTemplates_Tooltip:ClearLines()
		OptionTemplates_Tooltip:AddLine(self.TooltipTitle,0.2,0.4,1)
		if self.TooltipText then
			OptionTemplates_Tooltip:AddLine(self.TooltipText,1,1,1,1)
		end
		OptionTemplates_Tooltip:Show()
	end

end

function Slider_Template_OnLeave(self)

	self:SetScript("OnUpdate",nil)
	OptionTemplates_Tooltip:Hide()
	
end

function Slider_Template_EditBox_OnEnterPressed(self)

	local text = self:GetText()
	text = tonumber(text)
	local min, max = self:GetParent().Slider:GetMinMaxValues()
	local value = self:GetParent().Slider:GetValue()
	if not text then
		text = value
	end
	if text > max then
		text = max
	elseif text < min then
		text = min
	end
	
	text = format("%."..self:GetParent().Slider.Round.."f",text)
	
	self:GetParent().Slider:SetValue(text)
	
end

function Slider_Template_EditBox_OnEscapePressed(self)

	local text = self:GetParent().Slider:GetValue()
	text = format("%."..self:GetParent().Slider.Round.."f",text)
	self:SetText(text)
	
end

---------------------
-- Button Template --
---------------------

function Button_Template_OnLoad(self)

	self.Text:SetTextColor(1,1,1)	
	self.Highlight:SetAlpha(0.7)	
	
	if self:GetAttribute("text") then
		self.Text:SetText(self:GetAttribute("text"))	
	end
	
	if self:GetAttribute("resize") then
		local width = self.Text:GetStringWidth()
		self:SetWidth(width + 12)
		self:SetHeight(20)
	end
	
	if self:GetAttribute("CustomTexture") then
		self.Texture:SetTexture(self:GetAttribute("CustomTexture"))
		self.Highlight:SetTexture(self:GetAttribute("CustomTexture"))
	else
		self.Highlight:SetVertexColor(1, 1, 1, 0.3)
		self.Texture:SetVertexColor(0.5, 0.5, 0.5)
	end
	
	if self:GetAttribute("BorderIndex") then
		BorderTemplates_CreateBorder(self, self:GetAttribute("BorderIndex"))
	else
		BorderTemplates_CreateBorder(self, 2)
	end
	
	
	if self:GetAttribute("tooltiptitle") then
		self.TooltipTitle = self:GetAttribute("tooltiptitle")
	end
	
	
	if self:GetAttribute("tooltiptext") then
		self.TooltipText = self:GetAttribute("tooltiptext")
	end

	if self:GetAttribute("func") then
		self.Func = _G[self:GetAttribute("func")]
		if not self.Func then
			self.Func = self:GetAttribute("func")
		end
	end
	
	self["Reset"] = Button_Template_OnReset
	
	if self:GetAttribute("disabled") then
		self:Disable()
	end
	
	if self:GetAttribute("bordercolor") then
		local colorString = self:GetAttribute("bordercolor")
		local r, g, b = select(3, colorString:find("(.+),(.+),(.+)"))
		self.BorderR = r
		self.BorderG = g
		self.BorderB = b
		self:SetBorderColor(r, g, b)
	end
	
end

function Button_Template_OnReset(self)

	if self:GetAttribute("text") then
		self.Text:SetText(self:GetAttribute("text"))	
	end
	
	if self:GetAttribute("resize") then
		local width = self.Text:GetStringWidth()
		self:SetWidth(width + 12)
		self:SetHeight(20)
	end
	
	if self:GetAttribute("tooltiptitle") then
		self.TooltipTitle = self:GetAttribute("tooltiptitle")
	end
	
	
	if self:GetAttribute("tooltiptext") then
		self.TooltipText = self:GetAttribute("tooltiptext")
	end
	

	if self:GetAttribute("func") then
		self.Func = _G[self:GetAttribute("func")]
		if not self.Func then
			self.Func = self:GetAttribute("func")
		end
	end

end

function Button_Template_OnEnter(self)

	self.Highlight:Show()
	if self.TooltipFunc then
		OptionTemplates_Tooltip:SetOwner(self,"ANCHOR_LEFT")
		OptionTemplates_Tooltip:ClearLines()
		self.TooltipFunc()
		OptionTemplates_Tooltip:Show()
	elseif self.TooltipTitle then
		OptionTemplates_Tooltip:SetOwner(self,"ANCHOR_LEFT")
		OptionTemplates_Tooltip:ClearLines()
		OptionTemplates_Tooltip:AddLine(self.TooltipTitle,1,0.6,0)
		if self.TooltipText then
			OptionTemplates_Tooltip:AddLine(self.TooltipText,1,1,1,1)
		end
		OptionTemplates_Tooltip:Show()
	end

end

function Button_Template_OnLeave(self)

	self.Highlight:Hide()
	OptionTemplates_Tooltip:Hide()

end

function Button_Template_OnMouseDown(self)

	if self:IsEnabled() then
		self.Texture:ClearAllPoints()
		self.Texture:SetPoint("TOPLEFT",self,"TOPLEFT",2,-2)
		self.Texture:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",0,0)
		self.Highlight:ClearAllPoints()
		self.Highlight:SetPoint("TOPLEFT",self,"TOPLEFT",2,-2)
		self.Highlight:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",0,0)
	end

end

function Button_Template_OnMouseUp(self)

	if self:IsEnabled() then
		self.Texture:ClearAllPoints()
		self.Texture:SetPoint("TOPLEFT",self,"TOPLEFT",0,0)
		self.Texture:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",0,0)
		self.Highlight:ClearAllPoints()
		self.Highlight:SetPoint("TOPLEFT",self,"TOPLEFT",0,0)
		self.Highlight:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",0,0)
	end

end

function Button_Template_OnClick(self)

	if self.Func then
		if type(self.Func) == "string" then
			if _G[self.Func] then
				_G[self.Func](self)
			end
		else
			self.Func(self)
		end
	end

end

function Button_Template_OnDisable(self)

	self.Text:SetTextColor(0.4,0.4,0.4)
	self.Texture:SetVertexColor(0.2,0.2,0.2)
	self:HideBorder()
	
end

function Button_Template_OnEnable(self)

	self.Text:SetTextColor(1,1,1)
	self.Texture:SetVertexColor(0.5, 0.5, 0.5)
	self:ShowBorder()
	
end

---------------------------
-- Check Button Template --
---------------------------

function CheckButton_Template_OnLoad(self)

	self.Checked = false
	self["SetChecked"] = CheckButton_Template_SetChecked
	self["GetChecked"] = CheckButton_Template_GetChecked
	
	if self:GetAttribute("tooltiptitle") then
		self.TooltipTitle = self:GetAttribute("tooltiptitle")
	end
	
	if self:GetAttribute("tooltiptext") then
		self.TooltipText = self:GetAttribute("tooltiptext")
	end
	
	if self:GetAttribute("text") then
		self.Text:SetText(self:GetAttribute("text"))
	end
	
	if self:GetAttribute("func") then
		self.Func = _G[self:GetAttribute("func")]
	end
	
	if self:GetAttribute("checked") == true then
		self:SetChecked(1)
	end
	
	hooksecurefunc(self,"Disable", CheckButton_Template_OnDisable)
	hooksecurefunc(self,"Enable", CheckButton_Template_OnEnable)
	
	BorderTemplates_CreateBorder(self)
	self:SetBorderColor(0.8, 0.8, 0.8)
	
	if self:GetAttribute("disabled") then
		self:Disable()
	end

end

function CheckButton_Template_OnDisable(self)

	self.Text:SetTextColor(0.5, 0.5, 0.5)
	self:SetAlpha(0.7)

end

function CheckButton_Template_OnEnable(self)

	self.Text:SetTextColor(1, 1, 1)
	self:SetAlpha(1)

end

function CheckButton_Template_SetChecked(self,checked)

	if checked then
		self.Checked = true
		self.Texture:SetTexture("Interface\\Addons\\Library_OptionTemplates\\Textures\\ButtonOn")
	else
		self.Checked = false
		self.Texture:SetTexture("Interface\\Addons\\Library_OptionTemplates\\Textures\\ButtonOff")
	end

end

function CheckButton_Template_GetChecked(self)

	return self.Checked
	
end

function CheckButton_Template_OnEnter(self)

	if self.TooltipTitle then
		OptionTemplates_Tooltip:SetOwner(self,"ANCHOR_LEFT")
		OptionTemplates_Tooltip:ClearLines()
		OptionTemplates_Tooltip:AddLine(self.TooltipTitle,1,0.6,0)
		if self.TooltipText then
			OptionTemplates_Tooltip:AddLine(self.TooltipText,1,1,1,1)
		end
		OptionTemplates_Tooltip:Show()
	end
	
	self:SetBorderColor(1, 1, 1)

end

function CheckButton_Template_OnLeave(self)

	OptionTemplates_Tooltip:Hide()
	self:SetBorderColor(0.8, 0.8, 0.8)

end

function CheckButton_Template_OnMouseDown(self)

	if self:IsEnabled() then
		self.Texture:ClearAllPoints()
		self.Texture:SetPoint("TOPLEFT",self,"TOPLEFT",4,-4)
		self.Texture:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",-2,2)
	end

end

function CheckButton_Template_OnMouseUp(self)

	if self:IsEnabled() then
		self.Texture:ClearAllPoints()
		self.Texture:SetPoint("TOPLEFT",self,"TOPLEFT",2,-2)
		self.Texture:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",-2, 2)
	end

end

function CheckButton_Template_OnClick(self)

	if self:GetChecked() then
		self:SetChecked(false)
	else
		self:SetChecked(true)
	end
	
	if self.Func then
		self.Func(self)
	end

end


-----------------------
-- Dropdown Template --
-----------------------

function DropdownEntry_Template_OnLoad(self)

	BorderTemplates_CreateBorder(self)
	self:SetBorderColor(1.0, 1.0, 0.5)
	self:HideBorder()
	self.Highlight:SetVertexColor(0.1,0.8,1,0.7)
	
end

function DropdownEntry_Template_OnEnter(self)

	if self.Title then
		return
	end

	self.Highlight:Show()
	if self.Level == 3 then
		self:GetParent().Parent:GetParent().Parent:SetScript("OnUpdate", nil)
	end
	self:GetParent().Parent:SetScript("OnUpdate",nil) 
	
	if self.TooltipTitle then
		if self:GetLeft() < self:GetWidth() then
			OptionTemplates_Tooltip:SetOwner(self,"ANCHOR_RIGHT")
		else
			OptionTemplates_Tooltip:SetOwner(self,"ANCHOR_LEFT")
		end
		OptionTemplates_Tooltip:ClearLines()
		OptionTemplates_Tooltip:AddLine(self.TooltipTitle,1.0,0.6,0)
		if self.TooltipText then
			OptionTemplates_Tooltip:AddLine(self.TooltipText,1,1,1,1)
		end
		OptionTemplates_Tooltip:Show()
	end
	
	if self.hasArrow then
		Dropdown_ShowDropDown(self, nil, 2)
	else
		if not self.Level then
			OptionTemplates_DropdownMenu2:Hide()
			OptionTemplates_DropdownMenu2:SetScript("OnUpdate", nil)	
		end
	end

end

function DropdownEntry_Template_OnLeave(self)

	if self.Title then
		return
	end

	self.Highlight:Hide()
	OptionTemplates_Tooltip:Hide()
	
	self:GetParent().Parent.Close = GetTime() + 3
    self:GetParent().Parent:SetScript("OnUpdate",Dropdown_Template_OnUpdate)
    
    if self.Level == 3 then
		self:GetParent().Parent:GetParent().Parent.Close = GetTime() + 3
		self:GetParent().Parent:GetParent().Parent:SetScript("OnUpdate",Dropdown_Template_OnUpdate)
	end
	
end

function DropdownEntry_Template_OnClick(self)

	if self.IsPopUp then
		self:GetParent():Hide()
		if self.Level == 3 then
			self:GetParent().Parent:GetParent():Hide()
		end
	else
		local id = self:GetID()
		self.Location = 10
		self.Timer = 0
		self.Distance = -(((id - 1) * 20) + 2)
		self.Inc = ((self.Location - self.Distance) / 0.2) * 0.05
		self:SetScript("OnUpdate",DropdownEntry_Template_OnUpdate)
	end
	
	if self.Func then
		if self.IsPopUp then
			self.Func(self)
		else
			self:GetParent().Parent.Text:SetText(self.Text:GetText())
			self.Func(self)
			self:GetParent().Parent.Text:SetText("")
		end	
	end

end

function DropdownEntry_Template_OnUpdate(self, elapsed)

	self.Timer = self.Timer + elapsed
	if self.Timer > 0.05 then
		self.Distance = self.Distance + self.Inc
		self:ClearAllPoints()
		if self.Distance >= self.Location then
			self:SetPoint("TOP",self:GetParent(),"TOP",0,-(((self:GetID() - 1) * 16) + 2))
			self:GetParent().Parent.Text:SetText(self.Text:GetText())
			self:GetParent():Hide()
			self:GetParent().Parent:SetScript("OnUpdate",nil)
			self:SetScript("OnUpdate",nil)
		else
			self:SetPoint("TOP",self:GetParent(),"TOP",0,self.Distance)
		end
		self.Timer = 0
	end

end

function DropdownArrow_Templates_OnLoad(self)

	BorderTemplates_CreateBorder(self)

end

function DropdownArrow_Template_OnMouseDown(self)

	self.Texture:ClearAllPoints()
	self.Texture:SetPoint("TOPLEFT",self,"TOPLEFT",2,-2)
	self.Texture:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",0,0)

end

function DropdownArrow_Template_OnMouseUp(self)

	self.Texture:ClearAllPoints()
    self.Texture:SetPoint("TOPLEFT",self,"TOPLEFT",0,0)
	self.Texture:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",0,0)

end

function DropdownMenu_Template_CreateEntry(self, menu)

	menu.Buttons = menu.Buttons + 1
	local button = CreateFrame("Button",menu:GetName().."Entry"..menu.Buttons.."",menu,"OptionTemplates_DropdownEntryTemplate")
	button:SetPoint("TOP",menu,"TOP",0,-(((menu.Buttons - 1) * 16) + 2))
	button:SetID(menu.Buttons)
	
end

function Dropdown_Scroll_OnScroll()

	FauxScrollFrame_Update(OptionTemplates_DropdownMenu.ScrollFrame, table.getn(OptionTemplates_DropdownMenu.ScrollTable), OptionTemplates_DropdownMenu.Current, 20);

	for line = 1, OptionTemplates_DropdownMenu.Current do
		lineplusoffset = line + FauxScrollFrame_GetOffset(OptionTemplates_DropdownMenu.ScrollFrame);
		if lineplusoffset < (table.getn(OptionTemplates_DropdownMenu.ScrollTable) + 1) then
			_G["OptionTemplates_DropdownMenuEntry"..line..""]:Show()
			_G["OptionTemplates_DropdownMenuEntry"..line..""].Text:SetText(OptionTemplates_DropdownMenu.ScrollTable[lineplusoffset]["Text"])
			_G["OptionTemplates_DropdownMenuEntry"..line..""].TooltipTitle = OptionTemplates_DropdownMenu.ScrollTable[lineplusoffset]["TooltipTitle"]
			_G["OptionTemplates_DropdownMenuEntry"..line..""].TooltipText = OptionTemplates_DropdownMenu.ScrollTable[lineplusoffset]["TooltipText"]
			_G["OptionTemplates_DropdownMenuEntry"..line..""].IsPopUp = false
			if OptionTemplates_DropdownMenu.ScrollTable[lineplusoffset]["Disabled"] then
				_G["OptionTemplates_DropdownMenuEntry"..line..""]:Disable()
				_G["OptionTemplates_DropdownMenuEntry"..line..""].Text:SetTextColor(0.5, 0.5, 0.5)
			else
				_G["OptionTemplates_DropdownMenuEntry"..line..""]:Enable()
				if OptionTemplates_DropdownMenu.ScrollTable[lineplusoffset]["R"] then
					_G["OptionTemplates_DropdownMenuEntry"..line..""].Text:SetTextColor(OptionTemplates_DropdownMenu.ScrollTable[lineplusoffset]["R"], OptionTemplates_DropdownMenu.ScrollTable[lineplusoffset]["G"], OptionTemplates_DropdownMenu.ScrollTable[lineplusoffset]["B"]) 
				else
					_G["OptionTemplates_DropdownMenuEntry"..line..""].Text:SetTextColor(1,1,1)
				end
			end
			local func = OptionTemplates_DropdownMenu.ScrollTable[lineplusoffset]["Func"]
			if func and type(func) == "string" then
				func = _G[func]
			end
			_G["OptionTemplates_DropdownMenuEntry"..line..""].Func = func	
		else
			_G["OptionTemplates_DropdownMenuEntry"..line..""]:Hide()
		end
	end

end

function DropdownArrow_Template_OnClick(self)

	Dropdown_ShowDropDown(self:GetParent(), true)

end

function Dropdown_ShowDropDown(self, notPopUp, level)

	local menu
	if level then
		menu = _G["OptionTemplates_DropdownMenu"..level]
	else
		menu = OptionTemplates_DropdownMenu
	end

	if menu.Parent then
		menu.Parent:SetScript("OnUpdate",nil)
	end
	
	self.Menu = menu
	menu:ClearAllPoints()
	if not level then
		menu:SetPoint("TOP",self,"BOTTOM",0,-1)
	else
		menu:SetPoint("TOPLEFT", self, "TOPRIGHT", 2, 0)
	end
	menu.Parent = self
	menu:SetScale(self:GetEffectiveScale() / UIParent:GetScale())
	
	local maxHeight = self:GetBottom() - 10
	local maxCount = maxHeight / 16
	maxCount = floor(maxCount)
	local scrollEnabled = false
	
	local datatable = _G[self:GetAttribute("datatable")]
	if self:GetAttribute("datatable") then
		datatable = _G[self:GetAttribute("datatable")]
	elseif self.DataFunc then
		datatable = self.DataFunc(self)
	elseif self.DataTable then
		datatable = self.DataTable
	elseif self:GetAttribute("datafunc") then
		datatable = _G[self:GetAttribute("datafunc")](self)
	end
	
	if not datatable or table.getn(datatable) == 0 then
		return
	end
	
	menu.ScrollFrame:Hide()
	
	if notPopUp and maxCount < #datatable then
		while maxCount >= menu.Buttons do
			DropdownMenu_Template_CreateEntry(self, menu)
		end
		menu.Current = maxCount
		if menu.Buttons > OptionTemplates_DropdownMenu.Current then
			for i = (menu.Current + 1), menu.Buttons do
				_G[menu:GetName().."Entry"..i..""]:Hide()
			end
		end
		menu.ScrollTable = datatable
		menu.ScrollFrame:Show()
		scrollEnabled = true
	else		
		while #datatable >= menu.Buttons do
			DropdownMenu_Template_CreateEntry(self, menu)
		end
		menu.Current = #datatable
		if menu.Buttons > menu.Current then
			for i = (menu.Current + 1), menu.Buttons do
				_G[menu:GetName().."Entry"..i..""]:Hide()
			end
		end
	end
	
	local totalWidth
	for i = 1, menu.Current do
		_G[menu:GetName().."Entry"..i..""]:Show()
		_G[menu:GetName().."Entry"..i..""].Text:SetText(datatable[i]["Text"])
		_G[menu:GetName().."Entry"..i..""].TooltipTitle = datatable[i]["TooltipTitle"]
		_G[menu:GetName().."Entry"..i..""].TooltipText = datatable[i]["TooltipText"]
		if notPopUp then
			_G[menu:GetName().."Entry"..i..""].IsPopUp = false
		else
			_G[menu:GetName().."Entry"..i..""].IsPopUp = true
		end
		local func = datatable[i]["Func"]
		if func and type(func) == "string" then
			func = _G[func]
		end
		if datatable[i]["Checked"] then
			_G[menu:GetName().."Entry"..i..""].Borders[5]:Show()
			_G[menu:GetName().."Entry"..i..""].Borders[6]:Show()
		else
			_G[menu:GetName().."Entry"..i..""]:HideBorder()
		end
		if level then
			_G[menu:GetName().."Entry"..i..""].Level = level + 1
		end
		if datatable[i]["HasArrow"] then
			_G[menu:GetName().."Entry"..i..""].Arrow:Show()
			_G[menu:GetName().."Entry"..i..""].hasArrow = true
			if datatable[i]["DataFunc"] then
				_G[menu:GetName().."Entry"..i..""].DataFunc = datatable[i]["DataFunc"]
			else
				_G[menu:GetName().."Entry"..i..""].DataFunc = self.DataFunc
			end
			_G[menu:GetName().."Entry"..i..""].EntryValue = datatable[i]["EntryValue"]
			_G[menu:GetName().."Entry"..i..""].Level = 2
		else
			_G[menu:GetName().."Entry"..i..""].Arrow:Hide()
			_G[menu:GetName().."Entry"..i..""].hasArrow = false
		end
		if datatable[i]["Title"] then
			_G[menu:GetName().."Entry"..i..""].Title = true
			_G[menu:GetName().."Entry"..i..""].Func = nil
			_G[menu:GetName().."Entry"..i..""]:Enable()
			_G[menu:GetName().."Entry"..i..""].Text:SetTextColor(1, 0.6, 0)		
			_G[menu:GetName().."Entry"..i..""].Text:SetJustifyH("LEFT")
			_G[menu:GetName().."Entry"..i..""].Text:ClearAllPoints()
			_G[menu:GetName().."Entry"..i..""].Text:SetPoint("TOPLEFT", _G[menu:GetName().."Entry"..i..""], "TOPLEFT", 5, -5)
			_G[menu:GetName().."Entry"..i..""].Text:SetPoint("BOTTOMRIGHT", _G[menu:GetName().."Entry"..i..""], "BOTTOMRIGHT", -5, 5)
		else
			_G[menu:GetName().."Entry"..i..""].Title = false
			_G[menu:GetName().."Entry"..i..""].Func = func
			if notPopUp then
				_G[menu:GetName().."Entry"..i..""].Text:SetJustifyH("MIDDLE")	
			else
				_G[menu:GetName().."Entry"..i..""].Text:SetJustifyH("LEFT")	
			end
			_G[menu:GetName().."Entry"..i..""].Text:ClearAllPoints()
			_G[menu:GetName().."Entry"..i..""].Text:SetPoint("TOPLEFT", _G[menu:GetName().."Entry"..i..""], "TOPLEFT", 10, -5)
			_G[menu:GetName().."Entry"..i..""].Text:SetPoint("BOTTOMRIGHT", _G[menu:GetName().."Entry"..i..""], "BOTTOMRIGHT", -10, 5)
			if datatable[i]["Disabled"] then
				_G[menu:GetName().."Entry"..i..""]:Disable()
				_G[menu:GetName().."Entry"..i..""].Text:SetTextColor(0.5, 0.5, 0.5)
			else
				_G[menu:GetName().."Entry"..i..""]:Enable()
				if datatable[i]["R"] then
					_G[menu:GetName().."Entry"..i..""].Text:SetTextColor(datatable[i]["R"], datatable[i]["G"], datatable[i]["B"]) 
				else
					_G[menu:GetName().."Entry"..i..""].Text:SetTextColor(1,1,1)
				end
			end
		end
		
		
		if not totalWidth then
			totalWidth = _G[menu:GetName().."Entry"..i..""].Text:GetStringWidth() + 25
		elseif totalWidth < _G[menu:GetName().."Entry"..i..""].Text:GetStringWidth() + 25 then
			totalWidth = _G[menu:GetName().."Entry"..i..""].Text:GetStringWidth() + 25
		end	
	end
	
	if not self.DropdownWidth then
		if totalWidth < self:GetWidth() then
			 totalWidth = self:GetWidth()
		end
	else
		totalWidth = self.DropdownWidth
	end
	
	if scrollEnabled then
		totalWidth = self:GetWidth()
	end
	
	for i = 1, menu.Current do
		_G[menu:GetName().."Entry"..i..""]:SetWidth(totalWidth)
	end

	
	local height = menu.Current * 16 + 4
	
	self.Menu:SetWidth(totalWidth)
	self.Menu:SetHeight(height)
	
	if scrollEnabled then
		menu.ScrollFrame:SetWidth(totalWidth - 5)
		menu.ScrollFrame:SetHeight(height)
		Dropdown_Scroll_OnScroll()
	end

	if self.Menu:IsVisible() and not level then
		self.Menu:Hide()
		self:SetScript("OnUpdate",nil)
    else
		self:SetScript("OnUpdate",Dropdown_Template_OnUpdate)
		self.Close = GetTime() + 5
		self.Menu:Show()
		self.Menu:SetToplevel(true)
		self.Menu:SetFrameStrata("DIALOG")
		self.Menu:SetFrameLevel(self:GetFrameLevel() + 5)
		self.Menu:SetToplevel(true)
    end
    
    if not notPopUp and not level then
		local left = menu:GetLeft()
		local right = menu:GetRight()
		local bottom = menu:GetBottom()
		local offset = 0
		
		right = UIParent:GetWidth() - right
		
		if left < 0 then
			offset = (0 - left) + 10
		elseif right < 0 then
			offset = -((0 - right) + 10)
		end
		
		menu:ClearAllPoints()
		if bottom < 0 then
			menu:SetPoint("BOTTOM",self,"TOP",offset,1)
		else
			menu:SetPoint("TOP",self,"BOTTOM",offset,-1)
		end
	elseif level and not notPopUp then
		local right = menu:GetRight()
		local bottom = menu:GetBottom()
		local anchor = "LEFT"
		local anchorTo = "RIGHT"
		
		right = UIParent:GetWidth() - right
		
		if right < 0 then
			anchor = "RIGHT"
			anchorTo = "LEFT"
		end
		
		menu:ClearAllPoints()
		if bottom < 0 then
			menu:SetPoint("BOTTOM"..anchor,self,"BOTTOM"..anchorTo,2,0)
		else
			menu:SetPoint("TOP"..anchor,self,"TOP"..anchorTo,2,0)
		end
    end

end

function DropdownMenu_Template_OnLoad(self)

    self:SetFrameLevel(self:GetFrameLevel() + 1)
	self.Inc = 0.2
	self.Buttons = 0
	self.Current = 0
	
	local texture = self:CreateTexture(nil, "BACKGROUND")
	texture:SetTexture(0,0,0)
	texture:SetPoint("TOPLEFT", self, "TOPLEFT")
	texture:SetPoint("BOTTOMRIGHT", self, "BOTTOMRIGHT")
	BorderTemplates_CreateBorder(self)
	
end

function DropdownMenu_Template_OnShow(self)

	self.Timer = 0
	self.Alpha = 0
	self:SetAlpha(0)
	self:SetScript("OnUpdate",DropdownMenu_Template_OnUpdate)
	
end

function DropdownMenu_Template_OnUpdate(self, elasped)

	self.Timer = self.Timer + elasped
	if self.Timer > 0.05 then
		self.Alpha = self.Alpha + self.Inc
		if self.Alpha >= 1 then
			self:SetAlpha(1)
			self:SetScript("OnUpdate",nil)
		else
			self:SetAlpha(self.Alpha)
			self.Timer = 0
		end
	end

end

function Dropdown_Template_OnUpdate(self, elasped)

	if GetTime() >= self.Close then
		self:SetScript("OnUpdate",nil)
		self.Menu:Hide()
	end

end

function Dropdown_Template_OnLoad(self)

	self.Text:SetFont("Fonts\\FRIZQT__.TTF",10)
	self.Text:SetShadowColor(0,0,0)
	self.Text:SetShadowOffset(1,-1)
	self.Texture:SetVertexColor(0.5,0.5,0.5)
	BorderTemplates_CreateBorder(self)
	self["Disable"] = Dropdown_Template_OnDisable
	self["Enable"] = Dropdown_Template_OnEnable
	self.Text:SetHeight(11)
	self.Text:SetWidth(self:GetWidth())
	
	if self:GetAttribute("text") then
		self.Text:SetText(self:GetAttribute("text"))
	end
		
end

function Dropdown_Template_OnDisable(self)

	self.Arrow:Disable()
	self.Arrow.Texture:SetVertexColor(0.2,0.2,0.2)
	self.Texture:SetVertexColor(0.2,0.2,0.2)

end

function Dropdown_Template_OnEnable(self)

	self.Arrow:Enable()
	self.Arrow.Texture:SetVertexColor(1,1,1)
	self.Texture:SetVertexColor(0.5, 0.5, 0.5)

end

------------------
-- Tab Template --
------------------

function Tab_Template_OnLoad(self)

	self.Text:SetFont("Fonts\\FRIZQT__.TTF",10)
	self.Texture:SetVertexColor(0.2,0.2,0.2)
	self.Highlight:SetVertexColor(1,1,1,0.7)
	self:SetHeight(21)
	
	self["Activate"] = Tab_Template_Activate
	self["Deactivate"] = Tab_Template_Deactivate
	self["UpdateWidth"] =
		function (self)
			local width = self.Text:GetStringWidth()
			width = width + 12
			self:SetWidth(width)
		end
	
	if self:GetAttribute("datatable") then
		local datatable = self:GetAttribute("datatable")
		self.Text:SetText(_G[datatable][self:GetID()]["Text"])
		self.Tooltip = _G[datatable][self:GetID()]["Tooltip"]
		local func = _G[datatable][self:GetID()]["Func"]
		if func and type(func) == "string" then
			func = _G[func]
		end
		self.Func = func
		local width = self.Text:GetStringWidth()
		width = width + 12
		self:SetWidth(width)
	end
	
	BorderTemplates_CreateBorder(self)
	
	self.Borders[3]:Hide()
	self.Borders[4]:Hide()
	self.Borders[6]:Hide()

	self.Borders[7]:ClearAllPoints()
	self.Borders[8]:ClearAllPoints()

	self.Borders[7]:SetPoint("TOPLEFT", self.Borders[1], "BOTTOMLEFT", 0, 0)
	self.Borders[7]:SetPoint("BOTTOMLEFT", self.Borders[3],"TOPLEFT" , 0, -7)
	
	self.Borders[8]:SetPoint("TOPRIGHT", self.Borders[2], "BOTTOMRIGHT", 0, 0)
	self.Borders[8]:SetPoint("BOTTOMRIGHT", self.Borders[4], "TOPRIGHT", 0, -7)
end

function Tab_Template_Activate(self)

	self.Texture:SetTexture("Interface\\Addons\\Library_OptionTemplates\\Textures\\TabOn")
	self.Texture:SetDrawLayer("ARTWORK", 2)
	self.Texture:ClearAllPoints()
	self.Texture:SetPoint("TOPLEFT",self,"TOPLEFT",1,-2)
	self.Texture:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",-1,-2)
	self.Text:SetTextColor(1,1,1)
	self.Texture:SetVertexColor(0.7,0.7,0.7)
	self.Active = true
	self.Highlight:Hide()
	
end

function Tab_Template_Deactivate(self)

	self.Active = false
	self.Texture:SetTexture("Interface\\Addons\\Library_OptionTemplates\\Textures\\Background1")
	self.Texture:SetDrawLayer("ARTWORK", 0)
	self.Texture:ClearAllPoints()
	self.Texture:SetPoint("TOPLEFT",self,"TOPLEFT",1,-2)
	self.Texture:SetPoint("BOTTOMRIGHT",self,"BOTTOMRIGHT",-1,2)
	self.Text:SetTextColor(0.8,0.8,0.8)
	self.Texture:SetVertexColor(0.2,0.2,0.2)

end

function Tab_Template_OnEnter(self)

	if not self.Active then
		self.Highlight:Show()
	end
	
	if self.Tooltip then
		OptionTemplates_Tooltip:SetOwner(self,"ANCHOR_LEFT")
		OptionTemplates_Tooltip:ClearLines()
		OptionTemplates_Tooltip:AddLine(self.Tooltip,1,0.6,0)
		OptionTemplates_Tooltip:SetBackdropBorderColor(0.7,0.7,0.7)
		OptionTemplates_Tooltip:Show()
	end

end

function Tab_Template_OnLeave(self)

	self.Highlight:Hide()
	OptionTemplates_Tooltip:Hide()

end

function Tab_Template_OnClick(self)

	if not self.Active then
		self:Activate()
		if self.Func then
			self.Func(self)
		end
	end
	
end

-------------------
-- List Template --
-------------------

function ListEntry_Template_OnLoad(self)

	self.Highlight:SetVertexColor(1,1,1,0.7)

end

function ListEntry_Template_OnEnter(self)

	self.Highlight:Show()

end

function ListEntry_Template_OnLeave(self)

	self.Highlight:Hide()

end

function List_Template_OnLoad(self)


end

-----------------------------
-- Popup Selector Template --
-----------------------------

function PopupSelector_Create(name, parent, datatable, anchor, anchorTo, anchorRel, xOfs, yOfs, textname)

	local frame = CreateFrame("Frame", name, parent, "OptionTemplates_SelectorTemplate")
	local width = 0
	
	for i = 1, table.getn(datatable) do
		local entry = CreateFrame("Button",name.."Entry"..i.."",_G[name],"OptionTemplates_SelectorEntryTemplate")
		entry.Text:SetText(datatable[i]["Text"])
		if entry.Text:GetStringWidth() > width then
			width = entry.Text:GetStringWidth()
		end
		entry:SetID(i)
		entry.Func = datatable[i]["Func"]
		entry.TooltipTitle = datatable[i]["TooltipTitle"]
		entry.TooltipText = datatable[i]["TooltipText"]
		entry.Disabled = datatable[i]["Disabled"]
		if i == 1 then
			entry:SetPoint("TOP",name,"TOP",0,-5)
		else
			entry:SetPoint("TOP",name.."Entry"..(i-1).."","BOTTOM",0,-2)
		end
		entry:SetHeight(18)
	end
	
	for i = 1, table.getn(datatable) do
		_G[name.."Entry"..i..""]:SetWidth(width + 10)
	end

	frame:SetHeight((table.getn(datatable) * 18) + 10)
	frame:SetWidth(width + 10)
	frame["Update"] = PopupSelector_Template_Update
	
	if not anchor then
		anchor = "CENTER"
	end
	if not anchorRel then
		anchorRel = "CENTER"
	end
	if not anchorTo then
		anchorTo = parent
	end
	
	frame:SetPoint(anchor,anchorTo,anchorRel,0,0)
	
end

function PopupSelector_Template_OnShow(self)

	local index = 1

	while _G[self:GetName().."Entry"..index..""] do
		_G[self:GetName().."Entry"..index..""]:SetAlpha(0)
		_G[self:GetName().."Entry"..index..""]:SetFadeIn((index - 1) / 4)
		if _G[self:GetName().."Entry"..index..""].Disabled then
			if _G[self:GetName().."Entry"..index..""].Disabled() then
				_G[self:GetName().."Entry"..index..""]:Disable()
				_G[self:GetName().."Entry"..index..""].Texture:SetVertexColor(0.3,0.3,0.3)
			else
				_G[self:GetName().."Entry"..index..""]:Enable()
				_G[self:GetName().."Entry"..index..""].Texture:SetVertexColor(1,1,1)
			end
		end
		index = index + 1
	end

end

function PopupSelectorEntry_Template_SetFadeIn(self, fadeStart)

	self.Start = GetTime() + fadeStart
	self.Timer = 0
	self.Alpha = 0
	self.Inc = 0.15
	
	self:SetScript("OnUpdate",PopupSelectorEntry_Template_OnUpdate)

end

function PopupSelectorEntry_Template_OnUpdate(self, elasped)

	self.Timer = self.Timer + elasped
	if self.Timer > 0.05 then
		if GetTime() >= self.Start then
			self.Alpha = self.Alpha + self.Inc
		end
		if self.Alpha >= 1 then
			self:SetAlpha(1)
			self:SetScript("OnUpdate",nil)
		else
			self:SetAlpha(self.Alpha)
			self.Timer = 0
		end
	end

end

function PopupSelectorEntry_Template_OnEnter(self)

	self.Highlight:Show()
	 
	if self.TooltipTitle then
		OptionTemplates_Tooltip:SetOwner(self,"ANCHOR_LEFT")
		OptionTemplates_Tooltip:ClearLines()
		OptionTemplates_Tooltip:AddLine(self.TooltipTitle,1.0,0.6,0)
		if self.TooltipText then
			OptionTemplates_Tooltip:AddLine(self.TooltipText,1,1,1,1)
		end
		OptionTemplates_Tooltip:Show()
	end
	
	self:GetParent():SetScript("OnUpdate",nil)

end

function PopupSelector_Template_Update(self)

	local index = 1

	while _G[self:GetName().."Entry"..index..""] do
		if _G[self:GetName().."Entry"..index..""].Disabled then
			if _G[self:GetName().."Entry"..index..""].Disabled() then
				_G[self:GetName().."Entry"..index..""]:Disable()
				_G[self:GetName().."Entry"..index..""].Texture:SetVertexColor(0.3,0.3,0.3)
			else
				_G[self:GetName().."Entry"..index..""]:Enable()
				_G[self:GetName().."Entry"..index..""].Texture:SetVertexColor(1,1,1)
			end
		end
		index = index + 1
	end

end

function PopupSelector_Template_OnUpdate(self, elasped)

	if GetTime() >= self.Close then
		self:SetScript("OnUpdate",nil)
		self:Hide()
	end

end

----------------------
-- Border Templates --
----------------------


function BorderTemplates_CreateCorner(corner, parentFrame, borderID, blend)

	if not borderID then
		borderID = 1
	end
	
	if parentFrame:GetObjectType() == "Texture" or parentFrame:GetObjectType() == "FontString" then
		parentFrame = parentFrame:GetParent()
	end

	local texture = parentFrame:CreateTexture(nil, "ARTWORK", 1)
	texture:SetTexture("Interface\\Addons\\Library_OptionTemplates\\Textures\\Border"..borderID..".tga")
	texture:SetTexCoord(borders[corner]["L"], borders[corner]["R"], borders[corner]["T"], borders[corner]["B"])

	if blend then
		texture:SetBlendMode(blend)
	end
	
	return texture
	
end

function BorderTemplates_CreateStraight(straight, parentFrame, borderID, blend, tile)

	if not borderID then
		borderID = 1
	end
	
	if parentFrame:GetObjectType() == "Texture" or parentFrame:GetObjectType() == "FontString" then
		parentFrame = parentFrame:GetParent()
	end

	local texture = parentFrame:CreateTexture(nil, "ARTWORK", 0)
	texture:SetTexture("Interface\\Addons\\Library_OptionTemplates\\Textures\\Border"..borderID..".tga")
	
	texture:SetTexCoord(borders[straight]["L"], borders[straight]["R"], borders[straight]["T"], borders[straight]["B"])	
	
	if blend then
		texture:SetBlendMode(blend)
	end
	
	if tile then
		if straight == "TOP" or straight == "BOTTOM" then
			texture:SetHorizTile(true)
		else
			texture:SetVertTile(true)
		end
	end
	
	return texture

end


function BorderTemplates_CreateBorder(frame, borderID, blend, tile, xOffsetLeft, xOffsetRight, yOffsetTop, yOffsetBottom)

	local width = frame:GetWidth()
	local height = frame:GetHeight()
	local _shortHeight = false
	
	if not xOffsetLeft then
		xOffsetLeft = 0
	end
	
	if not xOffsetRight then
		xOffsetRight = 0
	end
	
	if not yOffsetTop then
		yOffsetTop = 0
	end
	
	if not yOffsetBottom then
		yOffsetBottom = 0
	end
	
	frame.Borders = {}
	
	local texture = BorderTemplates_CreateCorner("TOPLEFT", frame, borderID, blend)
	texture:SetWidth(8)
	texture:SetHeight(8)
	texture:SetPoint("TOPLEFT", frame, "TOPLEFT", -2 - xOffsetLeft, 2 + yOffsetTop)
	frame.Borders[1] = texture
	
	local texture = BorderTemplates_CreateCorner("TOPRIGHT", frame, borderID, blend)
	texture:SetWidth(8)
	texture:SetHeight(8)
	texture:SetPoint("TOPRIGHT", frame, "TOPRIGHT", 2 + xOffsetRight, 2 + yOffsetTop)
	frame.Borders[2] = texture
	
	local texture = BorderTemplates_CreateCorner("BOTTOMLEFT", frame, borderID, blend)
	texture:SetWidth(8)
	texture:SetHeight(8)
	texture:SetPoint("BOTTOMLEFT", frame, "BOTTOMLEFT", -2 - xOffsetLeft, -2 - yOffsetBottom)
	frame.Borders[3] = texture
	
	local texture = BorderTemplates_CreateCorner("BOTTOMRIGHT", frame, borderID, blend)
	texture:SetWidth(8)
	texture:SetHeight(8)
	texture:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 2 + xOffsetRight, -2 - yOffsetBottom)
	frame.Borders[4] = texture
	
	local texture = BorderTemplates_CreateStraight("TOP", frame, borderID, blend, tile)
	texture:SetHeight(8 * 6 / 14)
	texture:SetPoint("TOPLEFT", frame.Borders[1], "TOPRIGHT", 0, 0)
	texture:SetPoint("TOPRIGHT", frame.Borders[2], "TOPLEFT", 0, 0)
	frame.Borders[5] = texture
	
	local texture = BorderTemplates_CreateStraight("BOTTOM", frame, borderID, blend, tile)
	texture:SetHeight(8 * 6 / 14)
	texture:SetPoint("BOTTOMLEFT", frame.Borders[3], "BOTTOMRIGHT", 0, 0)
	texture:SetPoint("BOTTOMRIGHT", frame.Borders[4], "BOTTOMLEFT", 0, 0)
	frame.Borders[6] = texture

	local texture = BorderTemplates_CreateStraight("LEFT", frame, borderID, blend, tile)
	texture:SetWidth(8 * 6 / 14)
	texture:SetPoint("TOPLEFT", frame.Borders[1], "BOTTOMLEFT", 0, 0)
	texture:SetPoint("BOTTOMLEFT", frame.Borders[3],"TOPLEFT" , 0, 0)
	frame.Borders[7] = texture	
	
	local texture = BorderTemplates_CreateStraight("RIGHT", frame, borderID, blend, tile)
	texture:SetWidth(8 * 6 / 14)
	texture:SetPoint("TOPRIGHT", frame.Borders[2], "BOTTOMRIGHT", 0, 0)
	texture:SetPoint("BOTTOMRIGHT", frame.Borders[4], "TOPRIGHT", 0, 0)
	frame.Borders[8] = texture
	
	local metaTable = getmetatable(frame)

	 metaTable["__index"]["SetBorderColor"] = 
		function (self, r, g, b, a)
			for i = 1, 8 do
				if self.Borders and self.Borders[i] then
					self.Borders[i]:SetVertexColor(r, g, b, a)
				end
			end
		end
		
	 metaTable["__index"]["HideBorder"] =
		function (self)
			for i = 1, 8 do
				if self.Borders and self.Borders[i] then
					self.Borders[i]:Hide()
				end
			end
		end
		
	 metaTable["__index"]["ShowBorder"] =
		function (self)
			for i = 1, 8 do
				if self.Borders and self.Borders[i] then
					self.Borders[i]:Show()
				end
			end
		end
		
	 metaTable["__index"]["SetBorderTexture"] =
		function (self, id)
			for i = 1, 8 do
				if self.Borders and self.Borders[i] then
					self.Borders[i]:SetTexture("Interface\\Addons\\Library_OptionTemplates\\Textures\\Border"..id..".tga")
				end
			end
		end
		
	 metaTable["__index"]["SetBorderLevel"] =
		function (self, layer, subLevel)
			for i = 1, 8 do
				if self.Borders and self.Borders[i] then
					self.Borders[i]:SetDrawLayer(layer, subLevel)
				end
			end
		end		

end

-----------------------
-- EditBox Templates --
-----------------------

function EditBox_Template_OnEscapePressed(self)

	self:ClearFocus()
	if self.OnEscapePressed then
		self.OnEscapePressed(self)
	end

end

function EditBox_Template_OnEnterPressed(self)

    if self.OnEnterPressed then
		self.OnEnterPressed(self)
	end
	
    self:ClearFocus()
    
end

function EditBox_Template_OnLoad(self)

	self:SetTextInsets(10,0,0,0)
	self:SetAutoFocus(false)
	BorderTemplates_CreateBorder(self)
	
	if self:GetAttribute("letters") then
		self:SetMaxLetters(self:GetAttribute("letters"))
	end
	
	if self:GetAttribute("enterfunc") then
		self.OnEnterPressed = _G[self:GetAttribute("enterfunc")]
	end
	
	if self:GetAttribute("escapefunc") then
		self.OnEscapePressed = _G[self:GetAttribute("escapefunc")]
	end
	      
end

----------------------------
-- Tab Selector Templates --
----------------------------

local function TabSelector_Template_CreateEntry(self, count, label, title, icon)

	local entry = string.format("%s%s%d",self:GetName(),"Tab", count)

	local button = CreateFrame("Button", entry, self)
	button:SetHeight(15)
	
	local text = button:CreateFontString(nil, "OVERLAY")
	text:SetFont("Fonts\\FRIZQT__.TTF", 10)
	text:SetPoint("LEFT", button, "LEFT", 5, 0)
	text:SetText(label)
	text:SetJustifyH("LEFT")
	
	button:SetWidth(text:GetStringWidth() + 15)
	button.Text = text
	
	local bg = button:CreateTexture(nil, "BACKGROUND")
	button.BG = bg
	bg:SetTexture(0, 0, 0, 0.8)
	bg:SetAllPoints(true)
	
	local highlight = button:CreateTexture(nil, "OVERLAY")
	button.Highlight = highlight
	highlight:SetTexture(1, 0.62, 0, 0.4)
	highlight:SetAllPoints(true)
	highlight:SetBlendMode("ADD")
	highlight:Hide()
	
	if title then
		button.IsTitle = true
		button.Text:SetTextColor(1, 0.6, 0)
		button:SetWidth(button:GetWidth() + 20)
		local texture = button:CreateTexture(nil, "ARTWORK")
		texture:SetPoint("RIGHT", button, "RIGHT", -3, 0)
		texture:SetTexture(icon)
		texture:SetWidth(13)
		texture:SetHeight(13)
	end
	
	BorderTemplates_CreateBorder(button, 4)
	button:SetBorderColor(0, 0, 0, 0.6)
	button:Hide()
	button:SetID(count)
	
	button:SetPoint("TOPRIGHT", self, "BOTTOMRIGHT", -10, -3 - ((count - 1) * 17))
	button:SetScript("OnEnter", TabSelectorElement_Template_OnEnter)
	button:SetScript("OnLeave", TabSelectorElement_Template_OnLeave)

end

function TabSelector_Template_OnLoad(self)

	if self:GetAttribute("dataTable") then
		self.TabTable = _G[self:GetAttribute("dataTable")]
	elseif self:GetAttribute("dataFunc") then
		self.TabTable = _G[self:GetAttribute("dataTable")]()
	elseif self:GetParent() and self:GetParent():GetAttribute("TabData") then
		self.TabTable = _G[self:GetParent():GetAttribute("TabData")]
	end
	
	if self.TabTable then
		local entry
		local width = 0
		for i = 1, table.getn(self.TabTable) do
			TabSelector_Template_CreateEntry(self, i, self.TabTable[i]["Text"], self.TabTable[i]["Title"], self.TabTable[i]["Icon"])
			entry = string.format("%s%s%d",self:GetName(),"Tab", i)
			_G[entry].Tooltip = self.TabTable[i]["Tooltip"]
			if _G[entry]:GetWidth() > width then
				width = _G[entry]:GetWidth()
			end
		end
		for i = 1, table.getn(self.TabTable) do
			entry = string.format("%s%s%d",self:GetName(),"Tab", i)
			_G[entry]:SetWidth(width)
		end
	else
		self.TabTable = {}
	end

end

function TabSelector_Template_OnMouseDown(self)

	if table.getn(self.TabTable) <= 1 then
		return
	end

	for i = 1, table.getn(self.TabTable) do
		local entry = string.format("%s%s%d",self:GetName(),"Tab", i)
		_G[entry]:Show()
		if self.TabTable[i]["Disabled"] and self.TabTable[i]["Disabled"]() then
			_G[entry].Disabled = true
			_G[entry].Text:SetTextColor(0.5, 0.5, 0.5)
		elseif self.TabTable[i]["Title"] then
			_G[entry].Disabled = false
			_G[entry].Text:SetTextColor(1, 0.6, 0)
		else
			_G[entry].Disabled = false
			_G[entry].Text:SetTextColor(1, 1, 1)
		end
	end
	

end

function TabSelector_Template_OnMouseUp(self)

	if self.Selected then
		for i = 1, table.getn(self.TabTable) do
			if self.TabTable[i]["Panel"] then
				_G[self.TabTable[i]["Panel"]]:Hide()
			end
		end
		if self.TabTable[self.Selected]["TitleText"] and self:GetParent().TitleText:GetText() ~= self.TabTable[self.Selected]["TitleText"] then
			self:GetParent().TitleText:SetText(self.TabTable[self.Selected]["TitleText"])
			self:GetParent().UnderLine:SetWidth(self:GetParent().TitleText:GetStringWidth() + 10)
			self:GetParent().Spark:ClearAllPoints()
			self:GetParent().Spark:SetPoint("TOPLEFT", self:GetParent(), "TOPLEFT",self:GetParent().UnderLine:GetWidth() + 13, -35)
			self:GetParent().Spark1Ani:SetOffset(-(self:GetParent().UnderLine:GetWidth() - 18), 0)
			self:GetParent().Spark2Ani:SetOffset((self:GetParent().UnderLine:GetWidth() - 18), 0)
		end
		_G[self.TabTable[self.Selected]["Panel"]]:Show()
		_G[self.TabTable[self.Selected]["Panel"]].FadeIn:Play()
		self:GetParent().SparkMove:Play()
		self:GetParent().CurrentMenu = self.TabTable[self.Selected]["Panel"]
		self:GetParent().TabTitle:SetText(self.TabTable[self.Selected]["Text"])		
	end

	for i = 1, table.getn(self.TabTable) do
		local entry = string.format("%s%s%d",self:GetName(),"Tab", i)
		_G[entry]:Hide()
	end

end

function TabSelector_Template_OnEnter(self)

	self.Highlight:Show()
	if table.getn(self.TabTable) > 1 then
		OptionTemplates_Tooltip:SetOwner(self, "ANCHOR_LEFT")
		OptionTemplates_Tooltip:ClearLines()
		OptionTemplates_Tooltip:AddLine("Click And Hold", 1, 0.6, 0, 1)
		OptionTemplates_Tooltip:AddLine("Release over a menu to select it", 1, 1, 1,1)
		OptionTemplates_Tooltip:Show()
	end
	
end

function TabSelector_Template_OnLeave(self)

	self.Highlight:Hide()
	OptionTemplates_Tooltip:Hide()

end

function TabSelectorElement_Template_OnEnter(self)

	if self.Disabled then
		return
	end
	
	if self.IsTitle then
		return
	end
	
	self.Highlight:Show()
	self:GetParent().Selected = self:GetID()
	
	if self.Tooltip then
		OptionTemplates_Tooltip:SetOwner(self, "ANCHOR_LEFT")
		OptionTemplates_Tooltip:ClearLines()
		OptionTemplates_Tooltip:AddLine(self.Tooltip, 1, 0.6, 0, 1)
		OptionTemplates_Tooltip:Show()
	end
	
end

function TabSelectorElement_Template_OnLeave(self)

	self:GetParent().Selected = nil
	self.Highlight:Hide()
	OptionTemplates_Tooltip:Hide()

end


---------------------------
-- Option Menu Templates --
---------------------------

function OptionMenu_Template_OnLoad(self)

	table.insert(optionMenus, self:GetName())
	
	self:Hide()	
	self:SetPoint("RIGHT", UIParent, "RIGHT", -3, 0)
	self:SetScript("OnShow", OptionMenu_Template_OnShow)
	
	local title = self:GetAttribute("Title")
	if title then
		self.TitleText:SetText(title)
	end
	
	local icon = self:GetAttribute("Icon")

	if icon then
		self.Icon.Texture:SetTexture(icon)
		self.Icon.Highlight:SetTexture(icon)
		if self:GetAttribute("IconTexCoord") then
			local texCoords = {}
			for entry in string.gmatch(self:GetAttribute("IconTexCoord"), "[^%,]*") do
				table.insert(texCoords,tonumber(entry))
			end
			self.Icon.Texture:SetTexCoord(texCoords[1], texCoords[2], texCoords[3], texCoords[4])
			self.Icon.Highlight:SetTexCoord(texCoords[1], texCoords[2], texCoords[3], texCoords[4])
		end
	end

	BorderTemplates_CreateBorder(self, 2)	
	self:SetBorderColor(0,0,0)
	BorderTemplates_CreateBorder(self.Icon, 2)	
	self.Icon:SetBorderColor(0,0,0)
	self.BG:SetVertexColor(0.4, 0.4, 0.4)
		
	local texture = self:CreateTexture(nil, "ARTWORK", 0)
	texture:SetTexture("Interface\\Addons\\Library_OptionTemplates\\Textures\\Border1.tga")
	texture:SetTexCoord(borders["BOTTOM"]["L"], borders["BOTTOM"]["R"], borders["BOTTOM"]["T"], borders["BOTTOM"]["B"])	
	texture:SetWidth(self.TitleText:GetWidth() + 10)
	texture:SetHeight(4)
	texture:SetPoint("TOPLEFT", self.TitleText, "BOTTOMLEFT", 1, -2)
	texture:SetVertexColor(0.9, 0.9, 0.9)
	
	self.UnderLine = texture
	
	local spark = self:CreateTexture(nil, "OVERLAY")
	self.Spark = spark
	spark:SetWidth(22)
	spark:SetHeight(12)
	spark:SetPoint("TOPLEFT", self, "TOPLEFT",texture:GetWidth() + 13, -35)
	spark:SetBlendMode("ADD")
	spark:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark")
	spark:SetVertexColor(1, 0.6, 0)
	
	self.Spark = spark
	
	local group = spark:CreateAnimationGroup()
	self.SparkMove = group
	
	local ani = group:CreateAnimation("Translation")
	ani:SetOffset(-(texture:GetWidth() - 18), 0)
	ani:SetDuration(0.5)
	ani:SetOrder(1)
	
	local ani2 = group:CreateAnimation("Translation")
	ani2:SetOffset((texture:GetWidth() - 18), 0)
	ani2:SetDuration(0.5)
	ani2:SetOrder(2)
	
	self.Spark1Ani = ani
	self.Spark2Ani = ani2
	
	TabSelector_Template_OnLoad(self.TabSelector)
	
	if self.TabSelector.TabTable[1] then
		if self.TabSelector.TabTable[1]["Title"] then
			if self.TabSelector.TabTable[2] then
				_G[self.TabSelector.TabTable[2]["Panel"]]:Show()
				self.CurrentMenu = self.TabSelector.TabTable[2]["Panel"]	
				self.TabTitle:SetText(self.TabSelector.TabTable[2]["Text"])
			end
		else
			_G[self.TabSelector.TabTable[1]["Panel"]]:Show()	
			self.CurrentMenu = self.TabSelector.TabTable[1]["Panel"]	
			self.TabTitle:SetText(self.TabSelector.TabTable[1]["Text"])
		end
	end
	
	self.ScaleButton:SetBorderColor(0, 0, 0)
	self.SeperateButton:SetBorderColor(0, 0, 0)
	self.CloseButton:SetBorderColor(0, 0, 0)
	
	if self:GetAttribute("ScaleFunction") then
		self.ScaleButton:Show()
	else
		self.ScaleButton:Hide()
	end
	
	if self:GetAttribute("VersionFunction") then
		self.Version:Show()
	else
		self.Version:Hide()
	end
	
	if self:GetAttribute("OnLoad") then
		_G[self:GetAttribute("OnLoad")](self)
	end

end

function OptionMenu_Template_OnShow(self)

	if menuPanel_Seperated then
		MenuPanel_Template_UnSeperate(menuPanel_Seperated, true)
	end

	if not self.FirstOpen then
		self.FirstOpen = true
		if self:GetAttribute("ScaleFunction") then
			local scale = _G[self:GetAttribute("ScaleFunction")]()
			self:SetScale(scale)
		end
	end
		
	if self:GetAttribute("VersionFunction") then
		_G[self:GetAttribute("VersionFunction")]()
	end	
	
	if self:GetAttribute("UpdateFunction") then
		_G[self:GetAttribute("UpdateFunction")]()
	end		

	for i = 1, table.getn(sideMenus) do
		if sideMenus[i] == self:GetName() then
			sideMenus[i] = "Free"
			_G["OptionTemplates_SideMenu"..i]:Hide()
		elseif sideMenus[i] ~= "Free" and not _G["OptionTemplates_SideMenu"..i]:IsVisible() then
			_G["OptionTemplates_SideMenu"..i]:Show()
		end
	end
	
	topMenu = self:GetName()
	
	for i = 1, table.getn(optionMenus) do
		if optionMenus[i] ~= self:GetName() and _G[optionMenus[i]]:IsShown() and not _G[optionMenus[i]].Closing then
			_G[optionMenus[i]].FadeIn:Stop()
			self.Wait = true
			OptionMenu_Template_AddSideMenu(optionMenus[i])
		elseif optionMenus[i] ~= self:GetName() and _G[optionMenus[i]]:IsShown() and _G[optionMenus[i]].Closing then
			_G[optionMenus[i]].Wait = true
			_G[optionMenus[i]].Closing = false
			_G[optionMenus[i]]:Hide()
		end
	end

	if self.Wait then
		self:Hide()
	else
		topMenu = nil
		if not self.NoEffect then
			self.FadeIn:Play()
		end
	end
	
end

function OptionMenu_Template_Close(self)

	self:GetParent():Hide()

end

function OptionMenu_Template_OnClose(self)

	self.SparkMove:Stop()
	self.FadeIn:Stop()
	self.ForceClose:Stop()
	
	if self.Wait then
		self.Wait = false
		return
	end
	
	if not self.Minimized then
		for i = 1, table.getn(sideMenus) do
			if sideMenus[i] ~= "Free" and self:GetName() ~= sideMenus[i] and not _G[sideMenus[i]].Closing then				
				if not topMenu then
					_G[sideMenus[i]]:Show()
				else
					_G[topMenu]:Show()
					topMenu = nil
				end
				break
			end
		end
	end
	
	if self.Closing then
		if topMenu then
			_G[topMenu]:Show()
			topMenu = nil
		end
		self.Closing = false
	end
	
	if self:GetAttribute("HideFunction") then
		_G[self:GetAttribute("HideFunction")](self)
	end
	
end

function OptionMenu_Template_Scale_OnLoad(self)

	local texture = self:CreateTexture(nil, "BACKGROUND")
	texture:SetTexture(0.3, 0.3, 0.3)
	texture:SetAllPoints(true)
	
	BorderTemplates_CreateBorder(self, 3, nil, true)

end

function OptionMenu_Template_ScaleFunc(self)

	if OptionTemplates_OptionMenu_ScaleFrame:IsVisible() then
		OptionTemplates_OptionMenu_ScaleFrame:Hide()
		OptionTemplates_OptionMenu_ScaleFrameSliderFrame:SetScript("OnUpdate", nil)
	else
		local scale = _G[self:GetParent():GetAttribute("ScaleFunction")]()
		OptionTemplates_OptionMenu_ScaleFrame:ClearAllPoints()
		OptionTemplates_OptionMenu_ScaleFrameSliderFrame.Menu = self:GetParent()
		OptionTemplates_OptionMenu_ScaleFrameSliderFrameSlider:SetValue(scale)
		local x, y = GetCursorPosition()
		OptionTemplates_OptionMenu_ScaleFrame:SetPoint("CENTER", UIParent, "BOTTOMLEFT", x / UIParent:GetEffectiveScale(), y / UIParent:GetEffectiveScale())		
		OptionTemplates_OptionMenu_ScaleFrame:Show()
		OptionTemplates_OptionMenu_ScaleFrame:SetFrameLevel(self:GetFrameLevel() + 1)
		OptionTemplates_OptionMenu_ScaleFrame:SetClampedToScreen(true)
	end

end

function OptionMenu_Template_SeperateFunc(self)

	if self:GetParent().CurrentMenu then
		MenuPanel_Template_SeperationMode(_G[self:GetParent().CurrentMenu], true)
	end
	
end

function OptionMenu_Template_ScaleClose(self)

	self:GetParent():Hide()

end

function OptionMenu_Template_SelectScale(self)

	_G[self:GetParent().Menu:GetAttribute("ScaleFunction")](self:GetValue())

	self:GetParent().Menu:SetScale(self:GetValue())
	
end

local function OptionMenu_Template_CreateSideButton(name)

	local id = table.getn(sideMenus) + 1

	local button = CreateFrame("Button", "OptionTemplates_SideMenu"..id, UIParent)
	button:SetWidth(25)
	button:SetHeight(25)
	button:SetPoint("RIGHT", UIParent, "RIGHT", -(((id - 1) * 27) + 3), 239)
	button:Hide()
	
	local texture = button:CreateTexture(nil, "BACKGROUND")
	texture:SetAllPoints(true)
	
	local highlight = button:CreateTexture(nil, "OVERLAY")
	highlight:SetAllPoints(true)
	highlight:SetBlendMode("ADD")
	highlight:SetTexCoord(0.1,0.9,0.1,0.9)
	highlight:Hide()
	
	button.Texture = texture
	button.Highlight = highlight
	
	BorderTemplates_CreateBorder(button, 2)	
	button:SetBorderColor(0,0,0)
	
	button:SetScript("OnEnter", 
		function(self)
			if self.Tooltip then
				OptionTemplates_Tooltip:SetOwner(self, "ANCHOR_LEFT")
				OptionTemplates_Tooltip:ClearLines()
				OptionTemplates_Tooltip:AddLine(self.Tooltip, 1, 0.6, 0)
				OptionTemplates_Tooltip:Show()
			end
			self.Highlight:Show()
		end
	)
	button:SetScript("OnLeave",
		function(self)
			OptionTemplates_Tooltip:Hide()
			self.Highlight:Hide()
		end
	)
	
	button:SetScript("OnClick",
		function(self)
			_G[self.MenuName]:Show()
		end
	)
	
	return id

end

function OptionMenu_Template_AddSideMenu(name)

	local id

	for i = 1, table.getn(sideMenus) do
		if sideMenus[i] == "Free" then
			id = i
		end
	end
	
	if not id then
		id = OptionMenu_Template_CreateSideButton(name)
	end
	
	local side = _G["OptionTemplates_SideMenu"..id]
	local menu = _G[name]
	
	side.Texture:SetTexture(menu.Icon.Texture:GetTexture())
	local ULx, ULy, LLx, LLy, URx, URy, LRx, LRy = menu.Icon.Texture:GetTexCoord()
	side.Texture:SetTexCoord(ULx, ULy, LLx, LLy, URx, URy, LRx, LRy)
	side.Highlight:SetTexture(menu.Icon.Texture:GetTexture())
	side.Highlight:SetTexCoord(ULx, ULy, LLx, LLy, URx, URy, LRx, LRy)
	side.MenuName = name
	sideMenus[id] = name
	
	side.Tooltip = menu.TitleText:GetText()
	
	menu.Closing = true
	menu.FadeIn:Stop()
	menu.ForceClose:Play()
	menu.SideID = id
	
end

function MenuPanel_Template_OnLoad(self)

	self.EffectBorders = {}
	
	local corner = BorderTemplates_CreateCorner("TOPLEFT", self, 3, nil, true)
	corner:SetWidth(8)
	corner:SetHeight(8)
	corner:SetPoint("TOPLEFT", self, "TOPLEFT", -2, 2)
	self.EffectBorders[1] = corner

	local texture = BorderTemplates_CreateStraight("TOP", self, 3, nil, true)
	texture:SetHeight(8 * 6 / 14)
	texture:SetPoint("TOPLEFT", corner, "TOPRIGHT", 0, 0)
	texture:SetPoint("TOPRIGHT", self, "TOPRIGHT", 0, 0)
	self.EffectBorders[2] = texture
	
	local spark = self:CreateTexture(nil, "OVERLAY")
	spark:SetWidth(22)
	spark:SetHeight(12)
	spark:SetPoint("LEFT", texture, "RIGHT", -9, -1)
	spark:SetBlendMode("ADD")
	spark:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark")
	spark:SetVertexColor(1, 0.6, 0)
	self.EffectBorders[4] = spark
	
	local group = spark:CreateAnimationGroup()
	self.SparkMove1 = group
	
	local ani = group:CreateAnimation("Translation")
	ani:SetOffset(-(self:GetWidth() - 50), 0)
	ani:SetDuration(1)
	ani:SetOrder(1)
	
	local ani2 = group:CreateAnimation("Translation")
	ani2:SetOffset((self:GetWidth() - 50), 0)
	ani2:SetDuration(1)
	ani2:SetOrder(2)
	
	local texture = BorderTemplates_CreateStraight("LEFT", self, 3, nil, true)
	texture:SetWidth(8 * 6 / 14)
	texture:SetPoint("TOPLEFT", corner, "BOTTOMLEFT", 0, 0)
	texture:SetPoint("BOTTOMLEFT", self,"BOTTOMLEFT" , 0, 0)
	
	self.EffectBorders[3] = texture
	
	local spark = self:CreateTexture(nil, "OVERLAY")
	spark:SetWidth(22)
	spark:SetHeight(12)
	spark:SetPoint("TOP", texture, "BOTTOM", 1, 4)
	spark:SetBlendMode("ADD")
	spark:SetTexture("Interface\\CastingBar\\UI-CastingBar-Spark")
	spark:SetVertexColor(1, 0.6, 0)
	local group = spark:CreateAnimationGroup()
	self.SparkMove2 = group
	group:SetScript("OnFinished", function (self) MenuPanel_Template_Seperate(self:GetParent():GetParent()) end)
	
	local ani = group:CreateAnimation("Translation")
	ani:SetOffset(0, (self:GetHeight() - 50))
	ani:SetDuration(1)
	ani:SetOrder(1)
	ani:SetScript("OnFinished", function (self) self:GetParent():GetParent():GetParent().Parent.FadeOut:Play() end)
	
	local ani2 = group:CreateAnimation("Translation")
	ani2:SetOffset(0, -(self:GetHeight() - 50))
	ani2:SetDuration(1)
	ani2:SetOrder(2)
	
	self.EffectBorders[5] = spark
	
	BorderTemplates_CreateBorder(self, 2)
	self:HideBorder()
	
	self["Seperate"] = MenuPanel_Template_SeperationMode
	
	self:SetBorderColor(0, 0, 0)
	self.RestoreButton:SetBorderColor(0, 0, 0)

end

function MenuPanel_Template_SeperationMode(self, seperate)

	if not seperate then
		if self.Seperated then
			MenuPanel_Template_UnSeperate(self)
		end
	elseif seperate then
		if not self.Seperated and not self.Seperating then
			self.Seperating = true
			self.HideScript = self:GetScript("OnHide")
			self:SetScript("OnHide", nil)
			local parent = self:GetParent()
			menuPanel_Seperated = self
			self.Parent = parent
			local left, bottom
			left = self:GetLeft()
			bottom = self:GetBottom()
			self:SetParent(UIParent)
			self:SetScale(self.Parent:GetScale())
			self:ClearAllPoints()
			self:SetFrameStrata("FULLSCREEN_DIALOG")
			self:SetClampedToScreen(true)
			self:SetPoint("BOTTOMLEFT", UIParent, "BOTTOMLEFT", left, bottom)
			self.SparkMove1:Play()
			self.SparkMove2:Play()
		end
	end

end

function MenuPanel_Template_UnSeperate(self, forced)

	if not forced then
		self.HideScript = self:GetScript("OnHide")
		self:SetScript("OnHide", nil)
	end
	
	self.Seperating = nil
	menuPanel_Seperated = nil
	self:SetParent(self.Parent)
	self.Seperated = nil
	self.Parent = nil
	self:ClearAllPoints()
	self:SetScale(1)
	self:SetClampedToScreen(false)
	self:SetPoint("TOPLEFT", self:GetParent(), "TOPLEFT", 23, -72)
	self:SetScript("OnDragStart", nil)
	self:SetScript("OnDragStop", nil)
	self.BG:SetTexture(0, 0, 0, 0.5)
	self:RegisterForDrag()
	self:SetMovable(false)
	self:EnableMouse(false)
	self.EffectBorders[1]:Show()
	self.EffectBorders[2]:Show()
	self.EffectBorders[3]:Show()
	self.EffectBorders[4]:Show()
	self.EffectBorders[5]:Show()	
	self:HideBorder()
	self.RestoreButton:Hide()
	
	if not forced then
		self:GetParent().NoEffect = true
		self:GetParent():SetAlpha(1)
		self:GetParent():Show()
		self:GetParent().NoEffect = false
		self:SetScript("OnHide", self.HideScript)
		self.HideScript = nil
	end
	
end

function MenuPanel_Template_Seperate(self)

	self:ShowBorder()
	self.Seperated = true
	self:RegisterForDrag("LeftButton")
	self:SetMovable(true)
	self:EnableMouse(true)
	self.EffectBorders[1]:Hide()
	self.EffectBorders[2]:Hide()
	self.EffectBorders[3]:Hide()
	self.EffectBorders[4]:Hide()
	self.EffectBorders[5]:Hide()
	self.RestoreButton:Show()
	self.BG:SetTexture(0, 0, 0, 0.7)
	self:SetFrameStrata("DIALOG")
	self:SetScript("OnDragStart",
		function (self)
			self:StartMoving()
		end
	)
	
	self:SetScript("OnDragStop",
		function (self)
			self:StopMovingOrSizing()
		end
	)
	self:SetScript("OnHide", self.HideScript)
	self.HideScript = nil

end

function MenuPanel_Template_RestoreOnClick(self)

	MenuPanel_Template_UnSeperate(self:GetParent())

end

function MenuPanel_Template_OnShow(self)

	if self:GetAttribute("UpdateFunction") then
		_G[self:GetAttribute("UpdateFunction")]()
	end
	
end

---------------------------
-- Scroll Frame Template --
---------------------------

function OptionTemplates_ScrollFrame_OnMouseWheel(self, delta)

	local scrollBar = _G[self:GetName() .. "ScrollBar"];
	local stepValue = self.StepValue or (scrollBar:GetHeight() / 2)
	if ( delta > 0 ) then
		scrollBar:SetValue(scrollBar:GetValue() - stepValue);
	else
		scrollBar:SetValue(scrollBar:GetValue() + stepValue);
	end

end